package com.android.pandoid.aidl;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.android.pandoid.IAidlRemoteInterface;
import com.android.pandoid.IAidlSendMessageInterface;
import com.android.pandoid.IManagerAidlInterface;
import com.android.pandoid.IMessageReceiverListener;
import com.android.pandoid.R;
import com.android.pandoid.aidl.entity.MessageBean;
import com.android.pandoid.service.AidilRemoteService;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Date: 2020/8/28 11:02
 * Author: Tatum
 * Description: TODO
 */
public class ActivityConnectAidl extends AppCompatActivity {

    private ServiceConnection serviceConnection;

    private IManagerAidlInterface iManagerAidlInterface;

    private IAidlRemoteInterface iAidlRemoteInterface;
    private IAidlSendMessageInterface iAidlSendMessageInterface;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_aidl);
        //  使屏幕保持开启状态
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);


        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
//                iAidlRemoteInterface = IAidlRemoteInterface.Stub.asInterface(service);
                try {
                    iManagerAidlInterface=IManagerAidlInterface.Stub.asInterface(service);
                    iAidlRemoteInterface=IAidlRemoteInterface.Stub.asInterface(iManagerAidlInterface.getAidlServiceInterface(IAidlRemoteInterface.class.getSimpleName())) ;
                    iAidlSendMessageInterface=IAidlSendMessageInterface.Stub.asInterface(iManagerAidlInterface.getAidlServiceInterface(IAidlSendMessageInterface.class.getSimpleName()));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                iManagerAidlInterface=null;
                iAidlRemoteInterface=null;
                iAidlSendMessageInterface=null;
            }
        };
        bindService(new Intent(this, AidilRemoteService.class), serviceConnection, Service.BIND_AUTO_CREATE);

        findViewById(R.id.buttonConnect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iAidlRemoteInterface.connect();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.buttonIsConnected).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    boolean isConnected = iAidlRemoteInterface.isConnected();
                    Toast.makeText(ActivityConnectAidl.this, "" + isConnected, Toast.LENGTH_LONG).show();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.buttonDisConnect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iAidlRemoteInterface.disconnect();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

        findViewById(R.id.buttonSendMsg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    MessageBean messageBean=new MessageBean();
                    messageBean.setContent("主进程发送给子进程");
                    iAidlSendMessageInterface.sendMessage(messageBean);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.buttonRegisterMessageReceiver).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iAidlSendMessageInterface.registMessageRecieverListener(messageReceiverListener);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.buttonUnRegisterMessageReceiver).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iAidlSendMessageInterface.unregistMessageRecieverListener(messageReceiverListener);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private Handler handler=new Handler(Looper.getMainLooper());

    private IMessageReceiverListener messageReceiverListener=new IMessageReceiverListener.Stub() {
        @Override
        public void onReceiverMessage(MessageBean message) throws RemoteException {
            handler.post(new Runnable() {
                public void run() {
                    Log.d("yi", "-------get message form remote process-------");
                    Toast.makeText(ActivityConnectAidl.this, "主进程接收监听信息：" + message.getContent(), Toast.LENGTH_LONG).show();
                }
            });
        }
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindService(serviceConnection);
        serviceConnection = null;
    }
}
